package com.easpeed.comon.database.bean

import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey
import com.blankj.utilcode.util.TimeUtils
import java.text.SimpleDateFormat
import java.util.*

@Entity(tableName = "table_crash")
class CrashBean {


    /**
     * 自增主键
     */
    @PrimaryKey(autoGenerate = true)
    var id: Long = 0L


    /**
     * 创建时间 秒
     */
    var createTimeMills: Long = 0L

    @Ignore
    var createTimeMillsStr: String? = ""

    /**
     * 更新时间
     */
    var updateTimeMills: Long = 0L


    /**
     * 用户名
     */
    var loginName: String? = null

    /**
     * 日志类型
     */
    var type: String? = ""


    public fun init() {
        //创建时间
        try {
            this.createTimeMillsStr = TimeUtils.millis2String(
                checkMills(createTimeMills) * 1000,
                SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
            )
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 主要内容
     */
    var content: String? = ""

    constructor(type: String?, content: String?) {
        this.type = type
        this.content = content
        this.createTimeMills = System.currentTimeMillis() / 1000
        this.updateTimeMills = this.createTimeMills
        this.loginName = "zhangsan"
    }

    override fun toString(): String {
        return "CrashBean(createTimeMills=$createTimeMills, updateTimeMills=$updateTimeMills, type=$type, content=$content)"
    }

    private fun checkMills(time: Long): Long {
        if ("$time".length == 13) {
            return time / 1000
        }
        return time
    }

}